﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace yaTDL.Core.MVVM
{
    public class InputBoxViewModel:ViewModelBase
    {

        #region Fields

        RelayCommand _okCommand;
        RelayCommand _cancelCommand;

        string _title;        
        string _text;        

        bool _wasCanceled = false;

        List<string> textValues = new List<string>();

        #endregion


        #region Properties

        public ICommand OkCommand
        {
            get
            {
                if (_okCommand == null)
                {
                    _okCommand = new RelayCommand(p => this.okCommandExecute(p), p => this.okCommandCanExecute(p));
                }
                return _okCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new RelayCommand(p => this.cancelCommandExecute(p));
                }
                return _cancelCommand;
            }
        }


        public string Title
        {
            get { return _title; }
            set
            {
                if (value != _title)
                {
                    _title = value;
                    RaisePropertyChanged("Title");
                }
            }
        }

        public string Text
        {
            get { return _text; }
            set
            {
                if (value != _text)
                {
                    textValues.Add(value);

                    _text = value;

                    RaisePropertyChanged("Text");
                }
            }
        }


        public bool WasCanceled
        {
            get { return _wasCanceled; }
            private set { _wasCanceled = value; }
        }

        public string OriginalText
        {
            get { return textValues.First(x => x!=null); }
        }


        #endregion



        #region Command Methods

        private bool okCommandCanExecute(object parameter)
        {
            return String.IsNullOrEmpty(Text) ? false : true;
        }

        private void okCommandExecute(object parameter)
        {
            CloseView();
        }

        private void cancelCommandExecute(object parameter)
        {
            WasCanceled = true;
            CloseView();
        }

        #endregion

    }
}
